package com.yiwei.hdb.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yiwei.hdb.R;
import com.yiwei.hdb.api.Api;
import com.yiwei.hdb.api.bean.MiBaoResetBean;
import com.yiwei.hdb.util.Network;
import com.yiwei.hdb.util.RxLifeUtil;
import com.yiwei.hdb.util.ToastUtil;
import com.yiwei.hdb.util.UI;
import com.yiwei.hdb.view.FlowViewHorizontal;
import com.yiwei.hdb.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3XueXinMiBaoResetActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_tips1)
    TextView txtTips1;
    @BindView(R.id.txt_tips2)
    TextView txtTips2;
    @BindView(R.id.edit_pass)
    EditText editPass;
    @BindView(R.id.edit_repass)
    EditText editRepass;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.linearLayout)
    LinearLayout linearLayout;
    @BindView(R.id.step_view)
    FlowViewHorizontal stepView;
    private Unbinder unbinder;
    private String verifycode;
    private String account;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xuexin_question_reset_v3);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        String[] stepTitles = new String[]{"选择方式", "验证账户", "重置密码"};
        stepView.setProgress(3, 3, stepTitles, null);
        verifycode = getIntent().getStringExtra("verifycode");
        account = getIntent().getStringExtra("account");
        if (TextUtils.isEmpty(account)) {
            account = "";
        }
        txtTips2.setText("登录用户名" + account);
    }

    @OnTextChanged({R.id.edit_pass, R.id.edit_repass})
    void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(UI.toString(editPass)) && !TextUtils.isEmpty(UI.toString(editRepass))) {
            btnSubmit.setSelected(true);
        } else {
            btnSubmit.setSelected(false);
        }
    }

    public void getMiBaoReset(String password, String verifycode) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        MiBaoResetBean bean = new MiBaoResetBean();
        bean.password = password;
        bean.verifycode = verifycode;
        Api.api_service.getMiBaoReset(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(model -> {
                    ToastUtil.makeText(this, "设置新密码成功");
                    finish();
                });
    }

    private boolean codeViewInputCheck() {
        if (TextUtils.isEmpty(UI.toString(editPass))) {
            ToastUtil.makeText(getApplicationContext(), "新密码不能为空", Toast.LENGTH_SHORT);
            return false;
        }
        if (TextUtils.isEmpty(UI.toString(editRepass))) {
            ToastUtil.makeText(getApplicationContext(), "重复新密码不能为空", Toast.LENGTH_SHORT);
            return false;
        }

        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_submit:
                if (codeViewInputCheck()) {
                    getMiBaoReset(UI.toString(editPass), verifycode);
                }
                break;
        }
    }
}
